#include "sampleDebugger.h"
#include "image.h"
//#include "filter.h"
#include "math.h"

void SampleDebugger::renderSamples(char *samples_file, int sample_zoom) {

  // create an image
  Image image = Image(width*sample_zoom,height*sample_zoom);

  for (int i = 0; i < width; i++) {
    for (int j = 0; j < height; j++) {
      
      // make a dark grey checkerboard pattern
      for (int zi = i*sample_zoom; zi < (i+1)*sample_zoom; zi++) {
	for (int zj = j*sample_zoom; zj < (j+1)*sample_zoom; zj++) {
	  if (((i+j)%2)==0)
	    image.SetPixel(zi,zj,d_Vec3f(0.2,0.2,0.2));
	  else 
	    image.SetPixel(zi,zj,d_Vec3f(0,0,0));
	}
      }
      
      // draw the sample points
      for (int n = 0; n < num_samples; n++) {
	Sample s = getSample(i,j,n);
	d_Vec2f p = s.getPosition();
	assert (p[0] >= 0 && p[0] <= 1);
	assert (p[1] >= 0 && p[1] <= 1);
	int x = (int)floor((i+p[0])*sample_zoom);
	int y = (int)floor((j+p[1])*sample_zoom);
	assert (x >= 0);
	if (x >= width*sample_zoom) x = width*sample_zoom - 1;
	assert (y >= 0);
	if (y >= height*sample_zoom) y = height*sample_zoom - 1;
	image.SetPixel(x,y,s.getColor());
      }
    }
  }

  // save the image
  image.SaveTGA(samples_file);
}

SampleDebugger::SampleDebugger(int _width, 
			       int _height, 
			       int _num_samples) 
{ 
  width = _width;
  height = _height;
  num_samples = _num_samples;
  assert(width > 0 && height > 0 && num_samples > 0);
  samples = new Sample[width*height*num_samples];
}

SampleDebugger::~SampleDebugger()
{
  delete [] samples;
}
